package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.common.PageResult;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/specification")
public class SpecificationController
{
    @Reference(timeout = 10000)
    private SpecificationService specificationService;

    @GetMapping("/findByPage")
    public PageResult findByPage(Specification specification, Integer current, @RequestParam(defaultValue = "5") Integer pageSize)
    {
        return specificationService.findByPage(specification, current, pageSize);
    }

    @PostMapping("/save")
    public boolean save(@RequestBody Specification specification)
    {
        try
        {
            specificationService.save(specification);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @PostMapping("/update")
    public boolean update(@RequestBody Specification specification)
    {
        try
        {
            specificationService.update(specification);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @GetMapping("/findOptionList")
    public List<SpecificationOption> findOptionList(Long id)
    {
        try
        {
            return specificationService.findOptionList(id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/delete")
    public boolean delete(Long[] ids)
    {
        try
        {
            specificationService.deleteAll(ids);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    /** 查找规格id,name */
    @GetMapping("/findSpecificationList")
    public List<Map<String,Object>> findSpecificationList()
    {
        return specificationService.findIdNameList();
    }
}